package com.example.weather.utils;


import android.os.Handler;
import android.os.Message;

import com.thinkpage.lib.api.TPCity;
import com.thinkpage.lib.api.TPLifeSuggestion;
import com.thinkpage.lib.api.TPListeners;
import com.thinkpage.lib.api.TPWeatherDaily;
import com.thinkpage.lib.api.TPWeatherManager;
import com.thinkpage.lib.api.TPWeatherNow;

import java.util.Date;
import java.util.Map;

/**
 * Created by hanjun on 2016/7/26.
 */
public class RequestDataThread extends Thread {
    private Handler handler;
    private int messageType;
    private TPWeatherManager weatherManager;
    private Object object;

    public RequestDataThread(Handler handler, int messageType, TPWeatherManager weatherManager, Object object) {
        this.handler = handler;
        this.messageType = messageType;
        this.weatherManager = weatherManager;
        this.object = object;
    }

    @Override
    public void run() {
        if (messageType == ConstantsUtil.Get_Now_Weather_Message_Id) {
            GetNowWeatherMessage(handler, messageType, weatherManager, object);
        } else if (messageType == ConstantsUtil.Get_Three_Day_Weather_Message_Id) {
            GetThreeDayWeatherMessage(handler, messageType, weatherManager, object);
        }else if (messageType == ConstantsUtil.Get_Life_Notice_Message_Id) {
            GetLifeNoticeMessage(handler, messageType, weatherManager, object);
        }

    }


    private void GetNowWeatherMessage(final Handler handler, final int messageType, TPWeatherManager weatherManager, Object object) {
        String city = (String) object;
        weatherManager.getWeatherNow(new TPCity(city)
                , TPWeatherManager.TPWeatherReportLanguage.kSimplifiedChinese
                , TPWeatherManager.TPTemperatureUnit.kCelsius
                , new TPListeners.TPWeatherNowListener() {
                    @Override
                    public void onTPWeatherNowAvailable(TPWeatherNow weatherNow, String errorInfo) {
                        Message message = handler.obtainMessage();
                        message.what = messageType;
                        if (weatherNow != null) {
                            message.obj = weatherNow;
                            message.arg1 = 1;
                        } else {
                            message.obj = errorInfo;
                            message.arg1 = 0;
                        }
                        handler.sendMessage(message);
                    }
                });
    }

    private void GetThreeDayWeatherMessage(final Handler handler, final int messageType, TPWeatherManager weatherManager, Object object) {
        Map<String,Object> map = (Map<String,Object>)object;
        TPCity city = new TPCity((String)map.get("city"));
        Date startDate = new Date((String)map.get("startDate"));
        int days = (int)map.get("days");
        weatherManager.getWeatherDailyArray(city
                ,TPWeatherManager.TPWeatherReportLanguage.kSimplifiedChinese
                , TPWeatherManager.TPTemperatureUnit.kCelsius
                ,startDate,days,new TPListeners.TPWeatherDailyListener(){
                    @Override
                    public void onTPWeatherDailyAvailable(TPWeatherDaily[] tpWeatherDailies, String errorInfo) {
                        Message message = handler.obtainMessage();
                        message.what = messageType;
                        if (tpWeatherDailies != null) {
                            message.obj = tpWeatherDailies;
                            message.arg1 = 1;
                        } else {
                            message.obj = errorInfo;
                            message.arg1 = 0;
                        }
                        handler.sendMessage(message);
                    }
                });
    }

    private void GetLifeNoticeMessage(final Handler handler, final int messageType, TPWeatherManager weatherManager, Object object){
        TPCity city = new TPCity((String)object);
        weatherManager.getLifeSuggestion(city
                ,TPWeatherManager.TPWeatherReportLanguage.kSimplifiedChinese
        ,new TPListeners.TPLifeSuggestionListener(){

                    @Override
                    public void onLifeSuggestionAvailable(TPLifeSuggestion tpLifeSuggestion, String errorInfo) {
                        Message message = handler.obtainMessage();
                        message.what = messageType;
                        if (tpLifeSuggestion != null) {
                            message.obj = tpLifeSuggestion;
                            message.arg1 = 1;
                        } else {
                            message.obj = errorInfo;
                            message.arg1 = 0;
                        }
                        handler.sendMessage(message);
                    }
                });
    }
}
